package com.example.mybatispuls.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Date;

/**
 * @description:
 * @author: 朱超
 * @createDate: 2022/5/12
 * @version: 1.0
 * @remark：备注
 */
@EnableTransactionManagement
@Configuration // 配置类
public class MyBatisPlusConfig implements MetaObjectHandler {

    // 注册乐观锁插件
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

    //插入时间的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("excelTime", new Date(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }

    //更新时间的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}
